from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MainPage():

    def __init__(self, ui):
        self.ui = ui

    def create_page(self):
        main_widget = QWidget()

        main_widget.setStyleSheet('''
                                    QWidget {
                                        background-color: #D3D3D3;
                                    }                                             
                                    ''')
        main_layout = QHBoxLayout()

        # 左边的布局
        left_widget = QWidget()
        left_widget.setStyleSheet('''
                                    QWidget {
                                        background-color: #ffffff;
                                        border-radius: 30px;
                                    }                                             
                                    ''')

        # 标题
        img_title = QLabel("AiSuperTool Free" + self.ui.version)
        img_title.setFont(QFont('楷体', 15))
        img_title.setAlignment(Qt.AlignCenter)

        # 图片
        self.ui.img_label = QLabel()
        self.ui.img_label.setMinimumWidth(1400)
        self.ui.img_label.setMinimumHeight(600)
        self.ui.img_label.setPixmap(QPixmap('images/roses.jpg'))
        self.ui.img_label.setScaledContents(True)

        self.ui.label_message = QLabel("未加载数据集")
        self.ui.label_message.setFont(QFont('楷体', 12))
        self.ui.label_loss = QLabel("")
        self.ui.label_loss.setFont(QFont('楷体', 12))
        self.ui.label_accuracy = QLabel("")
        self.ui.label_accuracy.setFont(QFont('楷体', 12))

        # 载入进度条控件
        self.ui.pgb = QProgressBar()
        # 配置一个值表示进度条的当前进度
        self.ui.pv = 0
        self.ui.pgb.setMinimumWidth(1400)
        self.ui.pgb.setMinimumHeight(60)
        self.ui.pgb.setMinimum(0)
        self.ui.pgb.setMaximum(100)

        self.ui.label_classes_number = QLabel("")
        self.ui.label_classes_number.setFont(QFont('楷体', 12))
        self.ui.label_classes_detail = QLabel("")
        self.ui.label_classes_detail.setFont(QFont('楷体', 12))


        left_layout = QVBoxLayout()
        left_layout.addWidget(img_title)
        left_layout.addWidget(self.ui.img_label, 0, Qt.AlignCenter)
        left_layout.addWidget(self.ui.label_message, 0, Qt.AlignCenter)
        left_layout.addWidget(self.ui.pgb, 0, Qt.AlignCenter)
        left_layout.addWidget(self.ui.label_classes_number, 0, Qt.AlignLeft)
        left_layout.addWidget(self.ui.label_classes_detail, 0, Qt.AlignLeft)
        left_layout.addWidget(self.ui.label_loss, 0, Qt.AlignLeft)
        left_layout.addWidget(self.ui.label_accuracy, 0, Qt.AlignLeft)

        left_widget.setLayout(left_layout)

        # 右边的布局
        right_widget = QWidget()
        right_widget.setMaximumWidth(800)
        right_widget.setStyleSheet('''
                                    QWidget {
                                        background-color: #ffffff;
                                        border-radius: 30px;
                                    }                                             
                                    ''')

        font = QFont('楷体', 13)

        # 加载数据集按钮
        self.ui.btn_load_dataset = QPushButton("加载数据集")
        self.ui.btn_load_dataset.setStyleSheet('''
                                QPushButton {
                                    width: 550px;
                                    height: 120px;
                                    background-color: #87CEFA;
                                    border-radius: 60px;
                                }

                                QPushButton:hover {
                                    background-color: #ecf5ff;
                                    color: #409eff;
                                }

                                QPushButton:pressed, QPushButton:checked {
                                    border: 1px solid #3a8ee6;
                                    color: #409eff;
                                }
                                ''')
        self.ui.btn_load_dataset.clicked.connect(self.ui.load_dataset)
        self.ui.btn_load_dataset.setFont(QFont('楷体', 13))

        # 开始训练模型按钮
        self.ui.btn_start_train = QPushButton("开始训练模型")
        self.ui.btn_start_train.setStyleSheet('''
                                QPushButton {
                                     width: 550px;
                                    height: 120px;
                                    background-color: #87CEFA;
                                    border-radius: 60px;
                                }

                                QPushButton:hover {
                                    background-color: #ecf5ff;
                                    color: #409eff;
                                }

                                QPushButton:pressed, QPushButton:checked {
                                    border: 1px solid #3a8ee6;
                                    color: #409eff;
                                }
                                ''')
        self.ui.btn_start_train.clicked.connect(self.ui.start_train)
        self.ui.btn_start_train.setFont(QFont('楷体', 13))

        # 模型推理预测按钮
        btn_model_predict = QPushButton("模型推理预测")
        btn_model_predict.setStyleSheet('''
                                QPushButton {
                                    width: 550px;
                                    height: 120px;
                                    background-color: #ffb515;
                                    border-radius: 40px;
                                }

                                QPushButton:hover {
                                    background-color: #ecf5ff;
                                    color: #409eff;
                                }

                                QPushButton:pressed, QPushButton:checked {
                                    border: 1px solid #3a8ee6;
                                    color: #409eff;
                                }
                                ''')
        btn_model_predict.clicked.connect(self.ui.predict_img)
        btn_model_predict.setFont(font)
        # 导出模型文件按钮
        btn_output_model = QPushButton("导出模型文件")
        btn_output_model.setStyleSheet('''
                                QPushButton {
                                    width: 550px;
                                    height: 120px;
                                    background-color: #EE7942;
                                    border-radius: 40px;
                                }

                                QPushButton:hover {
                                    background-color: #ecf5ff;
                                    color: #409eff;
                                }

                                QPushButton:pressed, QPushButton:checked {
                                    border: 1px solid #3a8ee6;
                                    color: #409eff;
                                }
                                ''')
        btn_output_model.setFont(font)
        btn_output_model.clicked.connect(self.ui.output_model_to_computer)

        # 预测结果
        self.ui.result = QLabel(" ")
        self.ui.result.setFont(QFont('楷体', 15))
        # 准确率
        self.ui.accuracy = QLabel(" ")
        self.ui.accuracy.setFont(QFont('楷体', 15))

        right_layout = QVBoxLayout()  # 垂直布局
        # 添加加载数据集按钮
        right_layout.addWidget(self.ui.btn_load_dataset, 0, Qt.AlignCenter)
        right_layout.addSpacing(30)
        # 添加开始训练模型的按钮
        right_layout.addWidget(self.ui.btn_start_train, 0, Qt.AlignCenter)
        right_layout.addSpacing(250)

        # 模型推理预测
        right_layout.addWidget(btn_model_predict, 0, Qt.AlignCenter)
        right_layout.addSpacing(30)
        # 导出模型文件按钮
        right_layout.addWidget(btn_output_model, 0, Qt.AlignCenter)
        right_layout.addSpacing(60)

        right_layout.addWidget(self.ui.result, 0, Qt.AlignCenter)
        right_layout.addSpacing(20)
        right_layout.addWidget(self.ui.accuracy, 0, Qt.AlignCenter)

        right_layout.addStretch()
        right_widget.setLayout(right_layout)

        main_layout.addWidget(left_widget)
        main_layout.addWidget(right_widget)
        main_widget.setLayout(main_layout)

        return main_widget

